classdef ziPlotterData < EquipmentControl.ZI.ziData.ziData
    %ZIPLOTTERDATA Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        default_channelName
        default_sampleName
        channel_Name
        sample_Name
        
        keepOffset
        
    end
    
    methods
        function obj = ziPlotterData(data,varargin)
            %ZIPLOTTERDATA Construct an instance of this class
            %   Detailed explanation goes here
            obj@EquipmentControl.ZI.ziData.ziData(data);
            
            obj.default_channelName = 'demods';
            obj.default_sampleName = 'sample';
            obj.channel_Name = obj.default_channelName;
            obj.sample_Name = obj.default_sampleName;
        end
%%
        function obj = setChannelName(obj, chName)
            obj.channel_Name = chName;
        end
        function obj = setSampleName(obj, saName)
            obj.sample_Name = saName;
        end
        
        function data = getNodeData(obj, channelIndex, nodeName, varargin)
            p = inputParser;
            p.addParameter('KeepOffset', false, @islogical);
            p.parse(varargin{:});
            
            obj.keepOffset = p.Results.KeepOffset;
            
            if ischar(channelIndex)
                chIdx = str2double(channelIndex) + 1;
            else
                chIdx = channelIndex;
            end
            
            if ~iscell(nodeName)
                nodeNameCell = {nodeName};
            else
                nodeNameCell = nodeName;
            end
            
            isNodeR = strcmp(nodeNameCell, 'r');
            isNodeT = strcmp(nodeNameCell, 'theta');
            nodeName1 = nodeNameCell(~ (isNodeR+isNodeT) );
            
            data.time = obj.getTimeArray(chIdx);
            data.value = obj.getData(obj.channel_Name, chIdx, obj.sample_Name, 1, nodeName1);
            data.label = nodeNameCell;
            
            if any(isNodeR)
                r = obj.get_r_array(chIdx);
                data.value = [data.value r];
            end
            
            if any(isNodeT)
                r = obj.get_theta_array(chIdx);
                data.value = [data.value r];
            end

        end
        
        function r = get_r_array(obj, chIdx)
            xy = obj.getData(obj.channel_Name, chIdx, obj.sample_Name, 1, {'x', 'y'});
            r = sqrt(sum(xy.*xy, 2));
        end
        function theta = get_theta_array(obj, chIdx)
            xy = obj.getData(obj.channel_Name, chIdx, obj.sample_Name, 1, {'x', 'y'});
            theta = atan2(xy(:, 2),xy(:,1))/pi*180;
        end
        
        function timeArray = getTimeArray(obj, chIdx)
            time_stamp = obj.getData(obj.channel_Name, chIdx, obj.sample_Name, 1, 'timestamp');
            if obj.keepOffset
                offset = 0.0;
            else
                offset = double(time_stamp(1));
            end
            timeArray = (double(time_stamp) - offset ) ./ obj.clockbase;
        end
    end
end

